﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web
Imports System.Collections
Imports System.Web.UI.WebControls

Public Class ValidationClass

    Public Function CheckExistence(ByVal sKey As String, ByVal sValkey As String, ByVal sTable As String) As Boolean

        '   verifica che il codice cliente non sia già stato creato
        Dim connStr As String
        Dim query As String
        Dim conn As SqlConnection
        Dim Adapter As SqlDataAdapter
        Dim ds As DataSet

        CheckExistence = False

        connStr = ConfigurationManager.ConnectionStrings("MSSql12155ConnectionString").ConnectionString
        conn = New SqlConnection(connStr)

        query = "SELECT * FROM " & sTable & " WHERE " & sKey & "='" & sValkey & "'"

        Adapter = New SqlDataAdapter(query, conn)

        ds = New DataSet()
        Adapter.Fill(ds)

        If ds.Tables(0).Rows.Count = 0 Then
            CheckExistence = False
        Else
            CheckExistence = True
        End If
    End Function

End Class

Public Class Utilities

    Public Shared Sub CheckAut(ByVal PageLevel As Integer)

        If System.Configuration.ConfigurationManager.AppSettings("MYDEBUG") = "on" And HttpContext.Current.Session("persons_id") Is Nothing Then
            HttpContext.Current.Session("persons_id") = 1
            HttpContext.Current.Session("userLevel") = 4
            HttpContext.Current.Session("UserName") = "TEST USER"
            HttpContext.Current.Session("ForcedAccount") = 1
        End If

        If Int32.Parse(HttpContext.Current.Session("userLevel")) < PageLevel Then

            System.Diagnostics.Debug.Write(HttpContext.Current.Session("userLevel"))
            HttpContext.Current.Response.Redirect("/timereport/menu.aspx?msgtype=E&msgno=" & System.Configuration.ConfigurationManager.AppSettings("MSGNO_AUTHORIZATION_FAILED"))

        End If

    End Sub

    Public Shared Sub CreateMessageAlert(ByRef aspxPage As System.Web.UI.Page, _
                         ByVal strMessage As String, ByVal strKey As String)

        Dim strScript As String = "<script language=JavaScript>alert('" _
                                            & strMessage & "')</script>"

        If (Not aspxPage.IsStartupScriptRegistered(strKey)) Then
            aspxPage.RegisterStartupScript(strKey, strScript)
        End If

    End Sub

    Public Shared Sub ExportXls(ByVal sQuery As String)

        '   verifica che il codice cliente non sia già stato creato
        Dim conn As SqlConnection
        Dim Adapter As SqlDataAdapter
        Dim ds As DataSet = New DataSet
        Dim GridExp As DataGrid = New DataGrid

        conn = New SqlConnection(ConfigurationManager.ConnectionStrings("MSSql12155ConnectionString").ConnectionString)

        Adapter = New SqlDataAdapter(sQuery, conn)
        Adapter.Fill(ds, "export")

        GridExp.DataSource = ds.Tables("export").DefaultView
        GridExp.DataBind()

        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=Export.xls")
        HttpContext.Current.Response.Charset = ""

        '  // If you want the option to open the Excel file without saving then
        '  // comment out the line below
        '  // Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.ContentType = "application/vnd.xls"
        'create a string writer
        Dim stringWrite As New System.IO.StringWriter
        'create an htmltextwriter which uses the stringwriter
        Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)

        GridExp.RenderControl(htmlWrite)

        HttpContext.Current.Response.Write(stringWrite.ToString)
        HttpContext.Current.Response.End()

    End Sub

    Public Shared Function CheckboxListSelections(ByVal list As System.Web.UI.WebControls.CheckBoxList) As String

        Dim values As ArrayList = New ArrayList
        Dim counter As Integer
        CheckboxListSelections = ""

        counter = 0
        While counter < list.Items.Count

            If list.Items(counter).Selected Then
                CheckboxListSelections = CheckboxListSelections & IIf(CheckboxListSelections = "", ASPcompatility.FormatStringDb(list.Items(counter).Value), ", " & ASPcompatility.FormatStringDb(list.Items(counter).Value))
            End If

            counter = counter + 1

        End While

    End Function


End Class

Public Class CommonFunction

    Public Shared Function GetAuthor(ByVal Persons_id As Integer, ByVal ldate As Date) As String

        Dim connStr As String
        connStr = ConfigurationManager.ConnectionStrings("MSSql12155ConnectionString").ConnectionString

        Dim conn As SqlConnection
        conn = New SqlConnection(connStr)

        Dim cmd As SqlCommand = New SqlCommand("SELECT Name from Persons where Persons_id = " & Persons_id, conn)

        conn.Open()

        Dim dr As SqlDataReader = cmd.ExecuteReader()
        dr.Read()

        If dr.HasRows Then
            GetAuthor = dr("Name") & " " & String.Format("{0:d/M/yyyy HH:mm}", ldate)
        Else
            Return ""
        End If

    End Function

End Class

Public Class ASPcompatility

    Public Shared Function FormatStringDb(ByVal InputString As String) As String

        FormatStringDb = "'" & InputString & "'"

    End Function

    Public Shared Sub SelectMonths(ByRef DDL As System.Web.UI.WebControls.DropDownList)
        Dim i As Integer
        Dim lItem As ListItem

        ' elenco dei mesi con default il corrente
        For i = 1 To 12

            lItem = New ListItem(MonthName(i), i.ToString())
            DDL.Items.Add(lItem)

            If Month(Now()) = i Then
                DDL.SelectedIndex = i - 1
            End If
        Next

    End Sub

    Public Shared Sub SelectYears(ByRef DDL As System.Web.UI.WebControls.DropDownList)
        Dim i As Integer
        Dim lItem As ListItem

        For i = MyConstants.First_year To MyConstants.Last_year

            ' elenco di anni con default quello corrente
            lItem = New ListItem(i.ToString(), i.ToString())
            DDL.Items.Add(lItem)

            If Year(Now()) = i Then
                DDL.SelectedIndex = i - MyConstants.First_year
            End If
        Next
    End Sub

    Public Shared Function LastDay(ByVal Month As Integer, ByVal Year As Integer) As String
        Dim aDaysInMonth() As Integer = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

        LastDay = aDaysInMonth(Month - 1) & "/" & Month & "/" & Year

    End Function

    Public Shared Function FirstDay(ByVal Month As Integer, ByVal Year As Integer) As String

        FirstDay = "01" & "/" & IIf(Month > 9, Month, "0" & Month) & "/" & Year

    End Function

    Public Shared Function FormatDateDb(ByVal DateToConvert As String) As String

        Dim aDay As Integer
        Dim aMonth As Integer
        Dim aYear As Integer

        aDay = Day(DateToConvert)
        aMonth = Month(DateToConvert)
        aYear = Year(DateToConvert)

        If ConfigurationSettings.AppSettings("DATASOURCE") <> "CASA-PC\SQLEXPRESS" Then
            '	 MSSQL
            FormatDateDb = "'" & aMonth & "-" & aDay & "-" & aYear & "'"
        Else
            '	 MSSQL EXPRESS
            FormatDateDb = "'" & aDay & "-" & aMonth & "-" & aYear & "'"
        End If

    End Function

    Public Shared Function GetRows(ByVal sSQLstring As String) As System.Data.DataRowCollection

        Dim connStr As String
        Dim conn As SqlConnection
        Dim Adapter As SqlDataAdapter
        Dim ds As DataSet

        connStr = ConfigurationManager.ConnectionStrings("MSSql12155ConnectionString").ConnectionString
        conn = New SqlConnection(connStr)

        Adapter = New SqlDataAdapter(sSQLstring, conn)

        ds = New DataSet()
        Adapter.Fill(ds)

        If ds.Tables(0).Rows.Count = 0 Then
            GetRows = Nothing
        Else
            GetRows = ds.Tables(0).Rows
        End If

    End Function


End Class